﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using SaturniLibrary.Web;
using Eleanor.BLL;
using SaturniLibrary.Geo;

namespace Eleanor.Website.Utility.Filter
{
    public class ChineseIpFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if(RobotsUtility.IsRobot(filterContext.HttpContext.Request.UserAgent))
            {
                return;
            }           
            IController controller = filterContext.Controller;
            if ("Eleanor.Website.Controllers".Equals(controller.GetType().Namespace, StringComparison.OrdinalIgnoreCase))
            {
                bool hasPosternToken = false;
                int posternTokenId;
                string posternTokenString = filterContext.RequestContext.HttpContext.Request.QueryString["posterntoken"];
                if (string.IsNullOrWhiteSpace(posternTokenString))
                {
                    posternTokenId = SessionManager.Get<int>("posterntoken");
                }
                else
                {
                    int.TryParse(posternTokenString, out posternTokenId);
                    SessionManager.Set("posterntoken", posternTokenId);
                }

                if (posternTokenId > 0)
                {                    
                    PosternTokenBLL posternTokenBLL = new PosternTokenBLL();
                    dynamic posternTokenEntity = posternTokenBLL.Get(posternTokenId);
                    DateTime now = DateTime.Now;
                    if (posternTokenEntity != null && posternTokenEntity.StartTime <= now || posternTokenEntity.EndTime >= now)
                    {
                        hasPosternToken = true;
                    }
                }

                if (!hasPosternToken)
                {
                    bool isChineseIp = false;
                    string remoteIp = filterContext.RequestContext.HttpContext.Request.GetRemoteIp();
                    if (string.IsNullOrWhiteSpace(remoteIp))
                    {
                        isChineseIp = true;
                    }
                    else
                    {
                        try
                        {
                            FreeGeoIpResponse response = SessionManager.Get<FreeGeoIpResponse>("ipInfo");
                            if (response == null)
                            {
                                FreeGeoIpRequest request = new FreeGeoIpRequest();
                                request.IpOrHost = remoteIp;
                                FreeGeoIpGateway gateway = new FreeGeoIpGateway();
                                response = gateway.GetResponse(request);
                                SessionManager.Set("ipInfo", response);
                                isChineseIp = "cn".Equals(response.CountryCode, StringComparison.OrdinalIgnoreCase);
                            }
                        }
                        catch (Exception ex)
                        {

                        }
                    }

                    if (isChineseIp)
                    {
                        filterContext.RequestContext.HttpContext.Response.StatusCode = 404;
                        filterContext.RequestContext.HttpContext.Response.End();
                    }
                }
            }
        }
    }
}
